let LoginHandler = function () {
}

LoginHandler.prototype.listenSubmitEvent = function () {
    $('#submit-btn').on('click', function (event) {
        event.preventDefault()
        let email = $('input[name="email"]').val()
        let password = $('input[name="password"]').val()
        let remember = $('input[name="remember"]').prop('checked')
        zlajax.post({
            'url': '/login/',
            'data': {
                'email': email,
                'password': password,
                'remember': remember ? 1 : 0
            },
            'success': function (data) {
                if (data['code'] === 200) {
                    let token = data['data']['token']
                    let user = data['data']['user']
                    localStorage.setItem('JWT_TOKEN_KEY', token)
                    localStorage.setItem('USER_KEY', JSON.stringify(user))
                    window.location = '/'
                } else {
                    alert(data['message'])
                }
            }
        })
    })
}

LoginHandler.prototype.run = function () {
    this.listenSubmitEvent()
}

$(function () {
    let handler = new LoginHandler()
    handler.run()
})